<?php


   class TousuAction extends CommonAction{
   
      public function index(){
	  
	  
	    $re=$this->page('tousu',10,'id desc');
		
		$i=-1;
		foreach($re as $v){
		
		   $i++;
		   
		  $mypianku=M('mypianku')->where('id="%s"',$v['oid'])->find();
		  
		  $p=M('pianku')->where('id="%s"',$mypianku['pid'])->find();
		  
		  $re[$i]['title']=$p['title'];
		
		}
	  
	     $this->re=$re;
	  
	     $this->display();
	  
	  }
	  
	  
	  public function xiaoxi(){
	  
	    if($_POST){
		
		    //获取token
			 
			     $wx=M('wx')->where('id=1')->find();
                 $APPID=$wx['appid'];
                 $APPSECRET=$wx['appsecret'];
                 $mytoken=M('token')->where('id=1')->find();
              

             
				if(time()>$mytoken['time']){
				
				$TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;
				
				$json=file_get_contents($TOKEN_URL);
				$result=json_decode($json);
				
				
				$token=$result->access_token;
				
				$st['token']=$token;
				$st['time']=time()+3600;
				M('token')->where('id=1')->save($st);
				
				}else{
				
				  $token=$mytoken['token'];
				
				}
				
				
		
		   
		   $order=M('orderinfo')->where('id="%s"',$_POST['id'])->find();
		   
		   $openid=$order['openid'];
		   
		   
		   
		   $date=date('Y-m-d H:i:s',time());
		
		   $this->moban($token,$openid,$date,$_POST['val']);
		   
		   $this->ajaxReturn('1');
		
		}
	   
	  
	  
	  }
	  
	    public function moban($token,$openid,$time,$value){
		
		    $sys=M('system')->where('id=1')->find();
     	    $url  = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$token;
      $data = '{
                "touser":"'.$openid.'",
                "template_id":"'.$sys['mobanid'].'",
                "url":"",
                "topcolor":"#FF0000",
                "data":{
                "first": {
                "value":"我们提醒您",
                "color":"#FF3366"
                },
                "keyword1":{
                "value":"违规提醒",
                "color":"#173177"
                },
                "keyword2":{
                "value":"'.$time.'",
                "color":"#173177"
                },
                "remark":{
                "value":"'.$value.'",
                "color":"#339933"
                }    
                }
             }';
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST,1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
     }
  
   
   
   
   }